<template>
	<uni-popup ref="popup" type="bottom" :animation="true">
		<view class="p-boddy">
			<view class="header">
				<text>评论</text>
				<image src="/static/image/close.png" mode="widthFix" @click="closePopup" />
			</view>

			<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll" @scrolltolower="lower">
				<view class="comment-li" v-for="(item,i) of comments" :key="item.id">
					<image class="head-img" :src="item.user.userInfo.avatar" mode="widthFix" lazy-load />
					<view class="content">
						<view class="header">
							<text class="name">@{{item.user.nickname}}</text>
							<text class="date">{{item.created_at}}</text>
						</view>
						<view class="msg">{{item.content}}</view>
					</view>
				</view>
				<view class="loading">{{loadingText}}</view>
			</scroll-view>

			<!-- <view class="comment-input">
				<input v-model="commentMsg" type="text" placeholder="发表评论吧~" confirm-type="send" @confirm="commentConfirm">
			</view> -->
			<view class="comment-input">
				<view class="changeview" @click="isshowcom">
					选择你想评价的内容吧
				</view>

				<view class="comlist" v-if="showcom" :style="anim">
					<view class="title">
						<view class="content">
							评论列表
						</view>
						<view class="close">
							<image src="/static/image/close.png" mode="" @click="cancel"></image>
						</view>
					</view>
					<scroll-view scroll-y="true" class="cscroll" @scrolltolower="lowercomlist">
						<view class="scomment-li">
							<view class="content" v-for="(item,index) in temlist" :key="index" @click="evaluate(item)">
								· {{item.content}}
							</view>
						</view>
					</scroll-view>
				</view>
			</view>
			<!-- <uni-popup v-if="showcom" class="comlist" type="center" :animation="true">
				<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll" @scrolltolower="lower">
					<view class="comment-li" v-for="(item,i) of comments" :key="item.id">
						<view class="content">
							评论列表
						</view>
					</view>
					<view class="loading">{{loadingText}}</view>
				</scroll-view>
			</uni-popup> -->
		</view>
	</uni-popup>
</template>

<script>
	import uniPopup from '../../components/uni-popup/components/uni-popup/uni-popup.vue'
	import uniTransition from '../../components/uni-transition/components/uni-transition/uni-transition.vue'
	// 测试服务器

	export default {
		components: {
			uniPopup,
			uniTransition
		},
		data() {
			return {
				id: -1,
				scrollTop: 0,
				comments: [],
				commentMsg: '',
				loadingText: '',
				reqLock: false,
				itemInfo: {},
				listQuery: {
					page: 1,
					limit: 10
				},
				pagecount: 1,
				showcom: false,
				temlist: [],
				tpage: 1,
				tpagecount: 1,

				targetype: -1
			}
		},
		filters: {
			dataFilters(timeSpan) {
				let now = new Date(timeSpan),
					y = now.getFullYear(),
					m = now.getMonth() + 1,
					d = now.getDate();
				return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d) + " " + now.toTimeString().substr(0,
					8);
			}
		},
		methods: {
			showPopup(item) {
				// console.log(item);
				this.itemInfo = item;
				this.commentMsg = '';
				if (this.id != item.id) {
					if (this.targetype != -1) {
						this.resetPopup(item.video_id);
					} else {
						this.resetPopup(item.id);
					}
				}
				this.showcom = false

				this.$refs.popup.open();
			},
			cancel() {
				this.showcom = false
			},
			resetPopup(id) {
				this.scrollTop = 0;
				this.id = id;
				this.comments = [];
				this.listQuery.page = 1;
				this.getList();
			},
			closePopup() {
				this.$refs.popup.close();
			},
			getList() {
				if (this.reqLock) {
					return;
				}
				this.reqLock = true;
				this.loadingText = '正在加载中...';
				this.reqLock = false;
				let url = this.$siteInfo.apiroot + '?_mall_id=' + this.itemInfo.mall_id + '&r=' + this.$apiUrl.sixty
					.comment
				this.$request({
					url: url,
					data: {
						video_id: this.id,
						page: this.listQuery.page
					},
				}).then(res => {
					this.reqLock = false;
					if (res.code == 0) {
						this.pagecount = res.data.pagination.page_count
						if (res.data.pagination.total_count > 0) {
							this.comments = res.data.list
							if (this.listQuery.page < this.pagecount) {
								this.listQuery.page += 1;
								this.comments = this.comments.concat(res.data.list);
								// console.log(this.comments);
								this.loadingText = '加载更多...';
							} else {
								this.loadingText = '没有更多评论了';
							}
						} else {
							this.loadingText = '暂时没有评论呢，来抢占沙发吧~';
						}
					}
				}).catch(err => {
					console.log(err);
				})
			},
			getTemList() {
				let url = this.$siteInfo.apiroot + '?_mall_id=' + this.itemInfo.mall_id + '&r=' + this.$apiUrl.sixty
					.comment
				this.$request({
					url: url,
					data: {
						target_type: 1,
						page: this.tpage
					}
				}).then(res => {
					if (res.code == 0) {
						this.temlist = this.temlist.concat(res.data.list)
						this.tpagecount = res.data.pagination.page_count
					}
				}).catch(err => {
					console.log(err);
				})
			},
			lower(e) {
				this.getList();
			},
			lowercomlist() {
				if (this.tpagecount > this.tpage) {
					this.tpage++
					this.getTemList()
				}
			},
			evaluate(item) {
				let url = this.$siteInfo.apiroot + '?_mall_id=' + this.itemInfo.mall_id + '&r=' + this.$apiUrl.sixty
					.addcomment
				this.$request({
					url: url,
					data: {
						video_id: this.id,
						target_id: item.id
					},
					method: 'post'
				}).then(res => {
					if (res.code == 0) {
						this.showcom = false
						// this.itemInfo.commentNum++;
						this.$set(this.itemInfo, 'commentNum', parseInt(this.itemInfo.commentNum) + 1);
						// console.log(this.itemInfo);
						this.resetPopup(this.id);
						// this.getList()
						uni.showToast({
							title: '评论添加成功',
							icon: 'none'
						})
					} else {
						this.showcom = false
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(err => {
					this.showcom = false
					uni.showToast({
						title: err.msg,
						icon: 'none'
					})
					console.log(err);
				})
			},
			commentConfirm() {
				let msg = this.commentMsg;
				this.commentMsg = '';

				if (msg) {
					let userInfo = uni.getStorageSync('userInfo');
					this.$api.addComment({
						vId: this.id,
						msg,
						...userInfo
					}).then(res => {
						uni.showToast({
							title: '评论成功',
						});
						this.itemInfo.commentNum++;
						this.resetPopup(this.id);
					})
					let url = this.$siteInfo.apiroot + '?_mall_id=' + this.itemInfo.mall_id + '&r=' + this.$apiUrl.sixty
						.addcomment
					this.$request({
						url: url,
						data: {
							video_id: this.id
						},
						method: 'post'
					}).then(res => {
						// console.log(res);
						this.reqLock = false;
						if (res.code == 0) {
							if (res.data.pagination.total_count > 0) {
								if (this.listQuery.page < this.pagecount) {
									this.listQuery.page += 1;
									this.pagecount = res.data.pagination.page_count
									this.comments = this.comments.concat(res.data.list);
									this.loadingText = '加载更多...';
								} else {
									this.loadingText = '暂时没有评论呢，来抢占沙发吧~';
								}
							} else {
								this.loadingText = '暂时没有评论呢，来抢占沙发吧~';
							}
						}
					}).catch(err => {
						console.log(err);
					})
				} else {
					uni.showToast({
						title: '请输入评论~',
						icon: 'none'
					})
				}
			},
			isshowcom() {
				if (this.$store.state.user.accessToken === '' || this.$store.state.user.accessToken ===
					null) {
					this.$store.dispatch('user/accessToken');
					return;
				}
				this.showcom = !this.showcom
				if (this.showcom == true) {
					this.tpage = 1
					this.temlist = []
					this.getTemList()
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.p-boddy {
		background-color: rgba(39, 39, 39, .95);
		/* 兼容 iOS < 11.2 */
		padding-bottom: constant(safe-area-inset-bottom);
		/* 兼容 iOS >= 11.2 */
		padding-bottom: env(safe-area-inset-bottom);

		padding: 30rpx;
		height: 60vh;
		border-radius: 16rpx 16rpx 0 0;



		display: flex;
		flex-direction: column;

		.header {
			position: relative;
			font-weight: bold;
			width: 100%;
			height: 50rpx;
			font-size: 32rpx;
			color: #fff;

			display: flex;
			align-items: center;
			justify-content: center;

			image {
				position: absolute;
				right: 0;
				width: 40rpx;
				top: 50%;
				transform: translateY(-50%);
			}
		}

		.scroll {
			flex: 1;
			height: calc(60vh - 156rpx);
			padding: 32rpx 0;
		}

		.comment-input {
			position: relative;

			.changeview {
				width: 100%;
				font-size: 30rpx;
				height: 80rpx;
				line-height: 80rpx;
				color: #fff;
			}

			.comlist {
				width: 100%;
				height: 30vh;
				background-color: #FFFFFF;
				position: absolute;
				bottom: 80rpx;
				left: 0;
				transition: 1s height;
				border-radius: 16rpx 16rpx 0 0;
				padding: 24rpx;
				font-size: 28rpx;

				.title {
					font-weight: 600;
					border-bottom: 1rpx solid #ebebeb;
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding: 6rpx 0;

					.close {
						width: 30rpx;
						height: 30rpx;
						border-radius: 50%;

						image {
							width: 100%;
							height: 100%;
							border-radius: 50%;
						}
					}
				}

				.cscroll {
					height: 21vh;
					padding: 12rpx 0;
				}
			}
		}

		.comment-input input {
			width: 100%;
			font-size: 30rpx;
			height: 80rpx;
			line-height: 80rpx;
			color: #fff;
		}

		.scomment-li {
			line-height: 60rpx;
		}
	}

	.comment-li {
		display: flex;
		align-items: flex-start;
		color: #fff;
		width: 100%;

		.content {
			flex: 1;
		}

		.head-img {
			width: 80rpx;
			border-radius: 50%;
			flex-shrink: 0;
			margin-right: 10rpx;
		}

		.header {
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 26rpx;

			.date {
				margin-right: 30rpx;
				font-weight: normal;
				color: rgba(255, 255, 255, .8);
				font-size: 25rpx;
			}
		}

		.msg {
			font-size: 28rpx;
		}
	}

	.comment-li+.comment-li {
		margin-top: 40rpx;
	}

	.loading {
		// padding-top: 50rpx;
		padding: 32rpx 0;
		text-align: center;
		color: #fff;
		font-size: 28rpx;

		width: 100%;
		// height: 100%;
		// padding: 280rpx 0;
	}
</style>
